package cn.domypay.mail.control;

import java.util.Date;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import cn.domypay.mail.bean.EmailAttachment;
import cn.domypay.mail.bean.EmailMessage;
import cn.domypay.mail.bean.EmailUser;


import sun.misc.BASE64Encoder;

public class EmailHelper {
	
	/**
	 * 解析邮件，获取邮件详细信息
	 * @param session
	 * @param msg
	 * @return
	 * @throws Exception
	 */
	public static Message compose(Session session, EmailMessage msg)
			throws Exception {
		Message mail = new MimeMessage(session);
		if (msg.getFrom() != null)
			mail.setFrom(msg.getFrom().copyTo());
		if (msg.getTo() != null && msg.getTo().length > 0)
			mail.setRecipients(
					javax.mail.internet.MimeMessage.RecipientType.TO,
					convertEmailUserToInternetAddress(msg.getTo()));
		if (msg.getCc() != null && msg.getCc().length > 0)
			mail.setRecipients(
					javax.mail.internet.MimeMessage.RecipientType.CC,
					convertEmailUserToInternetAddress(msg.getCc()));
		if (msg.getBcc() != null && msg.getBcc().length > 0)
			mail.setRecipients(
					javax.mail.internet.MimeMessage.RecipientType.BCC,
					convertEmailUserToInternetAddress(msg.getBcc()));
		if (msg.getSubject() != null)
			mail.setSubject(msg.getSubject());
		if (msg.getSentDate() != null)
			mail.setSentDate(msg.getSentDate());
		else
			mail.setSentDate(new Date());
		MimeMultipart mimeMultipart = new MimeMultipart();
		String text = msg.getContent();
		String contentType = msg.getContentType();
		
		if (text != null) {
			MimeBodyPart textPart = new MimeBodyPart();
			if (contentType == null){
				textPart.setText(text);
			}else if (contentType.indexOf("text/plain") != -1){
				textPart.setText(text);
			}else if (contentType.indexOf("text/html") != -1) {
//				处理内容体里面的图片
//				ContentHandle cHandle = new ContentHandle();
//				text = cHandle.parseContent(mimeMultipart, text);
				contentType = "text/html;charset=GBK";
				textPart.setContent(text, contentType);
			} else {
				textPart.setText(text);
			}
			mimeMultipart.addBodyPart(textPart);
		}
		EmailAttachment attachments[] = msg.getAttachments();
		BASE64Encoder enc = new BASE64Encoder();
		if (attachments != null) {
			for (int i = 0; i < attachments.length; i++) {
				EmailAttachment attachment = attachments[i];
				MimeBodyPart attachmentPart = new MimeBodyPart();
				attachmentPart.setDataHandler(attachment.getDataHandler());
				
				attachmentPart.setFileName("=?GBK?B?"
						+ enc.encode((new String(attachment.getFileName())
								.getBytes("GBK"))) + "?=");
				mimeMultipart.addBodyPart(attachmentPart);
			}

		}
		mail.setContent(mimeMultipart);
		return mail;
	}
	
	public static InternetAddress[] convertEmailUserToInternetAddress(
			EmailUser users[]) {
		int length = 0;
		if (users != null)
			length = users.length;
		if (length <= 0)
			return new InternetAddress[0];
		InternetAddress ias[] = new InternetAddress[length];
		for (int i = 0; i < length; i++)
			ias[i] = users[i].copyTo();

		return ias;
	}

}
